package com.kuaimacode.kframework.mybatis.models.user;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.kuaimacode.kframework.common.Constant;
import com.kuaimacode.kframework.exception.ErrorEnum;
import com.kuaimacode.kframework.exception.ServerInternalErrorException;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

/**
 * @author 中元
 * @date 2019/03/18
 */
@TableName("user_order")
@Data
//@ApiModel(value="Order-订单")
public class Order implements Serializable {

	@TableField(exist = false)
	private static final long serialVersionUID = 1L;
	@TableId(value = "id", type = IdType.AUTO)
	private Long id;
	//@ApiModelProperty(value = "订单号")
	@TableField("order_no")
	private String orderNo;
	@ApiModelProperty(value = "总金额")
	@TableField("amount")
	private BigDecimal amount;
	@ApiModelProperty(value = "备注")
	@TableField("remark")
	private String remark;
	@ApiModelProperty(value = "订单类型 1-跑腿 2-万能帮办")
	@TableField("type")
	private Integer type;
	@ApiModelProperty(value = "订单状态 1-待支付 2-待接单 3-服务中 4-取货中  5-送货中 6-待评价 7-已完成 8-已取消  (跑腿有取送货状态，技术服务接单后是服务中)")
	@TableField("status")
	private Integer status;
	@ApiModelProperty(value = "取货地址")
	@TableField("from_address_id")
	private Long fromAddressId;
	@ApiModelProperty(value = "送货地址")
	@TableField("to_address_id")
	private Long toAddressId;
	@ApiModelProperty(value = "更新时间")
	//@TableField("update_at")
	private Date updateAt;
	@ApiModelProperty(value = "创建时间")
	//@TableField("create_at")
	private Date createAt;
	//@ApiModelProperty(value = "支付时间")
	@TableField("pay_at")
	private Date payAt;
	//@ApiModelProperty(value = "用户id")
	@TableField("user_id")
	private Long userId;
	//@ApiModelProperty(value = "删除状态 1-正常 2-删除")
	@TableField("delete_state")
	private Integer deleteState;
	@ApiModelProperty(value = "取货送货时间")
	@TableField("task_time")
	private Date taskTime;
	@ApiModelProperty(value = "类型id")
	@TableField("category_id")
	private Long categoryId;
	@ApiModelProperty(value = "类型名称")
	@TableField("category_name")
	private String categoryName;
	@ApiModelProperty(value = "重量 单位公斤")
	@TableField("weight")
	private Integer weight;
	@ApiModelProperty(value = "代收款")
	@TableField("collection_amount")
	private BigDecimal collectionAmount;
	@ApiModelProperty(value = "代收款手续费")
	@TableField("collection_fee")
	private BigDecimal collectionFee;
	@ApiModelProperty(value = "服务费")
	@TableField("service_fee")
	private BigDecimal serviceFee;
	@ApiModelProperty(value = "保价金额")
	@TableField("insurance")
	private BigDecimal insurance;
	@ApiModelProperty(value = "保价手续费")
	@TableField("insurance_fee")
	private BigDecimal insuranceFee;
	@ApiModelProperty(value = "保价赔付金额")
	@TableField("insurance_payment")
	private BigDecimal insurancePayment;

	@ApiModelProperty(value = "确认码")
	@TableField("verify_code")
	private String verifyCode;
	@ApiModelProperty(value = "距离 单位公里")
	@TableField("distance")
	private Float distance;

	@ApiModelProperty(value = "优惠券id")
	@TableField("coupon_id")
	private Long couponId;
	@ApiModelProperty(value = "其他费用 如深夜服务加价")
	@TableField("other_fee")
	private BigDecimal otherFee;
	@ApiModelProperty(value = "其他费用说明")
	@TableField("other_fee_instruction")
	private String otherFeeInstruction;
	@ApiModelProperty(value = "支付类型 1-微信支付 2-支付宝支付 3-微信小程序支付")
	@TableField("pay_type")
	private Integer payType;
	@ApiModelProperty(value = "预计送达时间")
	@TableField("forecast_delivery_time")
	private Date forecastDeliveryTime;
	//@ApiModelProperty(value = "实际送达时间")
	@TableField("delivery_time")
	private Date deliveryTime;
	@ApiModelProperty(value = "商品费用（随意购时生效）")
	@TableField("goods_amount")
	private BigDecimal goodsAmount;
	@ApiModelProperty(value = "万能帮办图片")
	@TableField("images")
	private String images;
	//@ApiModelProperty(value = "师傅id")
	@TableField("master_id")
	private Long masterId;
	//@ApiModelProperty(value = "分站id")
	@TableField("agent_id")
	private String agentId;
	@TableField("night_fee")
	private BigDecimal nightFee;
	@TableField("out_trade_no")
	private String outTradeNo;//订单唯一识别码
	@ApiModelProperty(value = "子类型")
	@TableField("child_type")
	private Integer childType;//子类型
	@ApiModelProperty(value = "支付流水号")
	@TableField("trans_no")
	private String transNo;//支付流水号
    @ApiModelProperty(value = "货物图片")
    @TableField("goods_images")
    private String goodsImages;//货物图片
	@ApiModelProperty(value = "退款流水号")
	@TableField("cancle_trans_no")
	private String cancleTransNo;//退款流水号
    @ApiModelProperty(value = "订单取消原因")
    @TableField("cancle_remark")
    private String cancleRemark;//订单取消原因

	@ApiModelProperty(value = "是否合作商家订单  1-是 0-否")
	@TableField("has_partner")
	private Integer hasPartner;//是否合作商家订单  1-是 0-否

	@ApiModelProperty(value = "合作商家订单数量")
	@TableField("partner_number")
	private Integer partnerNumber;//合作商家订单数量


	@ApiModelProperty(value = "合作商家每单基础费用")
	@TableField("partner_per_amount")
	private BigDecimal partnerPerAmount;//合作商家每单基础费用

	@ApiModelProperty(value = "超重费用")
	@TableField("overweight_fee")
	private BigDecimal overweightFee;//超重费用

	@ApiModelProperty(value = "万能帮办定价方式 1-定价 2-不定价")
	@TableField("price_way")
	private Integer priceWay;//万能帮办定价方式 1-定价 2-不定价

	@ApiModelProperty(value = "万能帮办上门费用（非定价时）")
	@TableField("onsite_fee")
	private BigDecimal onsiteFee;//万能帮办上门费用（非定价时）

	@ApiModelProperty(value = "是否立即发货 1-是 0-否")
	@TableField("has_instant")
	private Integer hasInstant;//是否立即发货 1-是 0-否

	@ApiModelProperty(value = "万能帮办费用(定价时为定价费用，非定价时为师傅输入)")
	@TableField("anyhelp_fee")
	private BigDecimal anyhelpFee;//万能帮办费用(定价时为定价费用，非定价时为师傅输入)

	@ApiModelProperty(value = "优惠券金额")
	@TableField("coupon_amount")
	private BigDecimal couponAmount;//优惠券金额

	@ApiModelProperty(value = "优惠券描述")
	@TableField("coupon_desc")
	private String couponDesc;//优惠券描述

	@ApiModelProperty(value = "详情模板类型 1-跑腿帮我送、帮我取  2-随意购普通 3-随意购附近购买 4-技术服务类定价  5-技术服务类不定价 6 -合作商家")
	@TableField("detail_template")
	private Integer detailTemplate;//详情模板类型

	@ApiModelProperty(value = "代收货款支付状态 1-未支付  2-已支付 3-支付失败")
	@TableField("collection_pay_status")
	private Integer collectionPayStatus;//代收货款支付状态

	@ApiModelProperty(value = "代收货款支付商户订单号")
	@TableField("collection_out_trade_no")
	private String collectionOutTradeNo;//代收货款支付商户订单号

	@ApiModelProperty(value = "代收货款支付流水号")
	@TableField("collection_trans_no")
	private String collectionTransNo;//代收货款支付流水号

	@ApiModelProperty(value = "技术服务费（不定价）支付状态 1-未支付  2-已支付 3-支付失败")
	@TableField("fee_pay_status")
	private Integer feePayStatus;//技术服务费（不定价）支付状态 1-未支付  2-已支付 3-支付失败

	@ApiModelProperty(value = "技术服务费（不定价）支付商户订单号")
	@TableField("fee_out_trade_no")
	private String feeOutTradeNo;//技术服务费（不定价）支付商户订单号

	@ApiModelProperty(value = "技术服务费（不定价）支付流水号")
	@TableField("fee_trans_no")
	private String feeTransNo;//技术服务费（不定价）支付流水号

	@ApiModelProperty(value = "派单师傅id")
	@TableField("dispatch_master")
	private Long dispatchMaster;//派单师傅id

	@ApiModelProperty(value = "节假日费用")
	@TableField("holiday_fee")
	private BigDecimal holidayFee;//节假日费用

	@ApiModelProperty(value = "节假日费用说明")
	@TableField("holiday_fee_instruction")
	private String holidayFeeInstruction;//节假日费用说明

/*	@ApiModelProperty(value = "技术服务费（不定价）")
	@TableField("fee")
	private BigDecimal fee;//技术服务费（不定价）*/

	@ApiModelProperty(value = "音频")
	@TableField("audio")
	private String audio;//音频

	@ApiModelProperty(value = "到达起点时间")
	@TableField("origin_time")
	private Date originTime;

	@ApiModelProperty(value = "到达终点时间")
	@TableField("final_time")
	private Date finalTime;

	@ApiModelProperty(value = "取消时间")
	@TableField("cancle_time")
	private Date cancleTime;

	@ApiModelProperty(value = "取消人 1-系统 2-个人")
	@TableField("cancle_by")
	private Integer cancleBy;

	@ApiModelProperty(value = "完成订单类型0-正常,1-强制")
	@TableField("finish_status")
	private Integer finishStatus;

	@ApiModelProperty(value = "到达起点类型0-正常,1-强制")
	@TableField("operator_origin")
	private Integer operatorOrigin;

	@ApiModelProperty(value = "到达终点类型0-正常,1-强制")
	@TableField("operator_final")
	private Integer operatorFinal;
	/**
	 * 校验信息
	 * @return
	 */
	public Boolean validateInfo(){
		Boolean rslt = true;
		if(this.getHasPartner() == Constant.IsOrNotEnum.IS.getCode()){
			//如果是合作商家一键下单
			if(this.getPartnerNumber() == null || this.getPartnerNumber() <=0){
				rslt = false;
				throw new ServerInternalErrorException(ErrorEnum.NO_PARTNER_NUMBER);
			}
			if(this.getTaskTime() == null){
				rslt = false;
				throw new ServerInternalErrorException(ErrorEnum.NO_TASKTIME);
			}
		}else{
			//其他订单
			/*if(this.getAmount() == null || this.getAmount().compareTo(BigDecimal.ZERO)<=0){
				rslt = false;
			}*/
			/*if(this.getAgentId() == null || this.getAgentId() <= 0l){
				rslt = false;
			}*/
			if(this.getType() == null || this.getType() <= 0){
				rslt = false;
				throw new ServerInternalErrorException(ErrorEnum.NO_ORDER_TYPE);
			}
			if(this.getChildType() == null || this.getChildType() <= 0){
				rslt = false;
				throw new ServerInternalErrorException(ErrorEnum.NO_ORDER_CHILD_TYPE);
			}
			//跑腿需要校验类别
			if(Constant.OrderTypeEnum.PAO_TUI.getCode() == this.getType()){
				if(this.getCategoryId() == null || this.getCategoryId() <= 0l){
					rslt = false;
					throw new ServerInternalErrorException(ErrorEnum.NO_CATEGORY);
				}
				//跑腿-随意购，起始地址为空时不校验订单距离
				if(!(Constant.PaotuiTypeEnum.ANY_BUY.getCode() == this.getChildType()) &&
						!(this.getFromAddressId() == null || this.getFromAddressId() <= 0l)){
					if(this.getDistance() == null || this.getDistance() <= 0.0f){
						rslt = false;
						throw new ServerInternalErrorException(ErrorEnum.NO_FROM_ADDRESS);
					}
				}
			}else{
				//万能帮办要校验师傅ID
				//if(this.getMasterId() == null || this.getMasterId() <= 0){
				//	throw new ServerInternalErrorException(ErrorEnum.MUST_CHOOSE_MASTER);
				//}


			}

			/*if(this.getServiceFee() == null || this.getServiceFee().compareTo(BigDecimal.ZERO)<=0){
				rslt = false;
			}*/

			//跑腿-随意购时附近购买，起始地址可为空
			/*if(!(Constant.OrderTypeEnum.PAO_TUI.getCode() == this.getType() &&
					Constant.PaotuiTypeEnum.ANY_BUY.getCode() == this.getChildType()) &&
					//万能帮办时起始地址也可以为空
					(Constant.OrderTypeEnum.PAO_TUI.getCode() == this.getType()) &&
					(this.getFromAddressId() == null || this.getFromAddressId() <= 0l)){
				rslt = false;
				throw new ServerInternalErrorException(ErrorEnum.NO_FROM_ADDRESS);
			}*/
			if(!(Constant.OrderTypeEnum.PAO_TUI.getCode() == this.getType() &&
					Constant.PaotuiTypeEnum.ANY_BUY.getCode() == this.getChildType()) &&
					//万能帮办时起始地址也可以为空
					(Constant.OrderTypeEnum.PAO_TUI.getCode() == this.getType()) ){

				if(this.getFromAddressId() == null || this.getFromAddressId() <= 0l){
					rslt = false;
					throw new ServerInternalErrorException(ErrorEnum.NO_FROM_ADDRESS);
				}
			}

			if(this.getToAddressId() == null || this.getToAddressId() <= 0l){
				rslt = false;
				throw new ServerInternalErrorException(ErrorEnum.NO_TO_ADDRESS);
			}
		}
		if(this.getTaskTime() == null){
			//if(new Date().after(this.getTaskTime())){
				throw new ServerInternalErrorException(ErrorEnum.NOT_SURE_TASKTIME);
			//}
		}
		if(rslt){
			return rslt;
		}else{
			throw new ServerInternalErrorException(ErrorEnum.ORDER_INFO_NOT_INCOMPLETE);
		}
	}
}
